package nyist.media.service.impl;

import cn.hutool.core.util.IdUtil;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.BatchStatus;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import lombok.extern.log4j.Log4j2;
import nyist.media.properties.QiniuProperties;
import nyist.media.service.QiniuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : dcf
 **/
@Service
@Log4j2
public class QiNiuServiceImpl implements QiniuService {

    @Autowired
    private QiniuProperties qiniuProperties;

    @Override
    public String upload(byte[] bytes) {
        Auth auth = Auth.create(qiniuProperties.getAccess_key(), qiniuProperties.getSecret_key());
        String key = IdUtil.simpleUUID();
        Configuration configuration = new Configuration();
        UploadManager uploadManager = new UploadManager(configuration);
        String upToken = auth.uploadToken(qiniuProperties.getBucket_name());
        try {
            Response response = uploadManager.put(bytes, key, upToken);
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return qiniuProperties.getDomain() + putRet.key;
        } catch (QiniuException ex) {
            log.error("文件上传失败" + ex.getMessage());
        }
        return null;
    }

    @Override
    public boolean delete(String imgUrl) {
        Configuration configuration = new Configuration();
        Auth auth = Auth.create(qiniuProperties.getAccess_key(), qiniuProperties.getSecret_key());
        BucketManager bucketManager = new BucketManager(auth, configuration);
        try {
            bucketManager.delete(qiniuProperties.getBucket_name(), imgUrl.substring(imgUrl.lastIndexOf("/") + 1));
            System.out.println("【photo】：" + imgUrl + "删除成功");
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            log.error("删除失败" + ex.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteList(String[] urls) {

        Configuration configuration = new Configuration();
        Auth auth = Auth.create(qiniuProperties.getAccess_key(), qiniuProperties.getSecret_key());
        BucketManager bucketManager = new BucketManager(auth, configuration);

        BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations();
        batchOperations.addDeleteOp(qiniuProperties.getBucket_name(), urls);
        try {
            Response response = bucketManager.batch(batchOperations);
            BatchStatus[] batchStatusList = response.jsonToObject(BatchStatus[].class);
            for (int i = 0; i < urls.length; i++) {
                BatchStatus status = batchStatusList[i];
                String key = urls[i];
                if (status.code == 200) {
                    return true;
                } else {
                    log.error(status.data.error);
                    return false;
                }
            }
        } catch (QiniuException e) {
            log.error("批量删除失败", e.getMessage());
        }
        return true;
    }

    @Override
    public boolean copy(String url, String newKey) {
        Configuration configuration = new Configuration();
        Auth auth = Auth.create(qiniuProperties.getAccess_key(), qiniuProperties.getSecret_key());
        BucketManager bucketManager = new BucketManager(auth, configuration);
        try {
            bucketManager.copy(qiniuProperties.getBucket_name(), url.substring(url.lastIndexOf("/") + 1), qiniuProperties.getBucket_name(), newKey);
        } catch (QiniuException ex) {
            log.error("文件复制失败", ex.response.toString());
            return false;
        }
        return true;
    }
}
